package com.backend.vse.ws;


import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * WebSocket 控制器，用于处理实验相关操作。
 * 该控制器提供匹配用户和开始实验的端点。
 */
@Slf4j
@RestController
@RequestMapping("/exp8")
public class AIbuyerController {
    private static AIbuyer AIbuyer=new AIbuyer();
    @PostMapping("/seller/connect")
    public Map<String, Object> getSellerOffer()
    {

        boolean match=true;
        Map<String, Object> result = new HashMap<>();
        result.put("match",match);
        return result;
    }
    @GetMapping("/seller/offerSituation")
    public Map<String, Object> handleSubmitPrice(@RequestParam String situation)
    {
        Random random = new Random();
        int min,max;
        if(situation.equals("车况好")){
            min=70000;
            max=90000;
            AIbuyer.setSituationValue(generateRandomPrice(min,max,random));
        }
        if(situation.equals("车况差")){
            min=70000;
            max=80000;
            AIbuyer.setSituationValue(generateRandomPrice(min,max,random));
        }
        Map<String, Object> result = new HashMap<>();
        result.put("situationValue",AIbuyer.getSituationValue());
        result.put("message", "Price updated successfully");

        return result;
    }
    private int generateRandomPrice(int min, int max, Random random)
    {
        if (max > min) {
            return random.nextInt(max - min + 1) + min;
        } else {
            throw new IllegalArgumentException("Max value must be greater than Min value");
        }
    }
    @GetMapping("/seller/offerBadprice")
    public Map<String, Object> Badsituation(@RequestParam double price)
    {
        if(price<=AIbuyer.getSituationValue()){
            AIbuyer.setBuy("true");
        }
        if(price>AIbuyer.getSituationValue()){
            AIbuyer.setBuy("2");
        }
        Map<String, Object> result = new HashMap<>();
        result.put("buy",AIbuyer.getBuy());
        result.put("message", "Price updated successfully");

        return result;
    }
    @GetMapping("/seller/offerGoodprice")
    public Map<String, Object> Goodsituation(@RequestParam double price)
    {
        if(price<=AIbuyer.getSituationValue()){
            AIbuyer.setBuy("true");
        }
        if(price>AIbuyer.getSituationValue()){
            AIbuyer.setBuy("2");
        }
        Map<String, Object> result = new HashMap<>();
        result.put("buy",AIbuyer.getBuy());
        result.put("message", "Price updated successfully");

        return result;
    }

}
